package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPackersProjectTeamBinding;
import com.sgb.kjwl.view.event.ProjectTeamEvent;
import com.sgb.kjwl.view.ui.fragment.packers.PackersProjectTeamFragment;
import com.sgb.kjwl.view.widget.PakersProjectTeamPopWindow;
import com.sgb.kjwl.viewmodel.packers.PackersProjectTeamModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.PickSelectEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kobe577590
 * @date 2020/6/11
 * description:工程工队
 */
public class PackersProjectTeamActivity extends AppActivity {

    PackersProjectTeamModel viewModel;

    private ActivityPackersProjectTeamBinding binding;
    private String cid = "";
    private List<PickSelectEntity> cidList = new ArrayList<>();
    private final List<PickSelectEntity> typeData = new ArrayList<>();
    private final List<PickSelectEntity> scaleData = new ArrayList<>();
    private final List<PickSelectEntity> statusData = new ArrayList<>();
    private final List<PickSelectEntity> teamData = new ArrayList<>();

    private PakersProjectTeamPopWindow teamPopWindow;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerPackersComponent.builder().appComponent(appComponent)
//                .packersModule(new PackersModule(this))
//                .build()
//                .inject(this);
    }

    public static void start(Context context,String id) {
        Intent intent = new Intent();
        intent.putExtra("cid",id);
        intent.setClass(context, PackersProjectTeamActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        binding = DataBindingUtil.setContentView(this, R.layout.activity_packers_project_team);
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(view -> finish());
        binding.rightView.setOnClickListener(view -> showFilterPop());

        //更多
        teamPopWindow = new PakersProjectTeamPopWindow(getActivity());
        teamPopWindow.setBgView(binding.bgPopGrayLayout);
        teamPopWindow.setResuleListener(new PakersProjectTeamPopWindow.ProjectTeamPopListener() {
            @Override
            public void setResultListener(String cid, String type, String scale, String team, String status,String name) {
                Log.v("filter_",cid+"=="+type+"=="+scale+"=="+team+"=="+status);
                if(null!=cidList && cidList.size()>0){
                    for(int i=0;i<cidList.size();i++){
                        if(cid.equals(cidList.get(i).getNo())){
                            changeType(i);
                            ProjectTeamEvent event = new ProjectTeamEvent();
                            event.setCidNo(cid);
                            event.setTypeNo(type);
                            event.setScalNo(scale);
                            event.setTeamNo(team);
                            event.setStatusNo(status);
                            event.setScalName(name);
                            EventBus.getDefault().postSticky(event);
                            break;
                        }
                    }
                }
            }

            @Override
            public void setResetResultListener(String cid, String type, String scale, String team, String status,String name) {
                if(null!=cidList && cidList.size()>0){
                    changeType(0);
                    for(int i=0;i<cidList.size();i++){
                        ProjectTeamEvent event = new ProjectTeamEvent();
                        event.setCidNo(cidList.get(i).getNo());
                        event.setTypeNo(type);
                        event.setScalNo(scale);
                        event.setTeamNo(team);
                        event.setStatusNo(status);
                        event.setScalName(name);
                        EventBus.getDefault().postSticky(event);
                    }
                }
            }
        });
    }

    @Override
    protected void init() {
        cid = getIntent().getStringExtra("cid");
        viewModel.getFilterTypeModel(cid);
        viewModel.getScaleData();
        setStatusData();
        setTeamData();
    }

    public void showTypeView(List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) {
            return;
        }

        cidList = data;

        ArrayList<Fragment> mFragments = new ArrayList<>();
        int size = data.size();
        String[] titles = new String[size];
        for (int i = 0; i < size; i++) {
            mFragments.add(PackersProjectTeamFragment.newInstance(cid,data.get(i).getNo(), i));
            titles[i] = data.get(i).getProjectscale();
        }
        FragPagerAdapter pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length - 1);
        binding.tablayout.setViewPager(binding.viewpager, titles);

        if(null!=cidList && cidList.size()>0){
            viewModel.getFilterTypeData(cidList.get(0).getNo());
        }

        binding.tablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                teamPopWindow.setCid(cidList.get(position).getNo());
            }

            @Override
            public void onTabReselect(int position) {}
        });

        teamPopWindow.setCid(cidList.get(0).getNo());
    }

    public void changeType(int position) {
        binding.viewpager.setCurrentItem(position);
    }

    private void showFilterPop(){
        teamPopWindow.showPopwindowRight(binding.rightView);
        teamPopWindow.setData(cidList, typeData, scaleData, teamData, statusData);
    }

    public void setTypeData(List<PickSelectEntity> data) {
        typeData.clear();
        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("0");
        entity.setProjectscale("全部");
        typeData.add(0,entity);
        typeData.addAll(data);
    }

    public void setStatusData() {
        statusData.clear();
        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("-1");
        entity.setProjectscale("全部");
        PickSelectEntity entity1 = new PickSelectEntity();
        entity1.setNo("1");
        entity1.setProjectscale("可出工");
        PickSelectEntity entity2 = new PickSelectEntity();
        entity2.setNo("0");
        entity2.setProjectscale("不可出工");
        statusData.add(entity);
        statusData.add(entity1);
        statusData.add(entity2);
    }

    public void setScaleData(List<PickSelectEntity> data) {
        scaleData.clear();
        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("0");
        entity.setProjectscale("全部");
        scaleData.add(0,entity);
        scaleData.addAll(data);
    }

    public void setTeamData() {
        teamData.clear();
        PickSelectEntity entity = new PickSelectEntity();
        entity.setNo("-1");
        entity.setProjectscale("全部");
        PickSelectEntity entity1 = new PickSelectEntity();
        entity1.setNo("0");
        entity1.setProjectscale("个人");
        PickSelectEntity entity2 = new PickSelectEntity();
        entity2.setNo("1");
        entity2.setProjectscale("企业");
        teamData.add(entity);
        teamData.add(entity1);
        teamData.add(entity2);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(ProjectTeamEvent event) {
        if ("000".equals(event.getCidNo())) {
            if(null!=teamPopWindow){
                teamPopWindow.setType(event.getTypeNo());
                teamPopWindow.setScale(event.getScalNo());
                teamPopWindow.setTeam(event.getTeamNo());
                teamPopWindow.setStatus(event.getStatusNo());
            }
        }
        EventBus.getDefault().removeStickyEvent(event);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
